#!/usr/bin/perl -w

use strict;
use File::Find;
use Digest::MD5;

my %files;
my $wasted = 0;
my $arg = $ARGV[0];
$ARGV[0] or $arg = ".";

find(\&check_file, $arg);



my $duplicate_save_dir = "$arg"."/duplicate_files";

print "#!/bin/bash\n\n";


print "shellout(){\necho \"ERROR \$? in \$1 commande\";\nexit 2;\n}\n\n";


print "mkdir \"$duplicate_save_dir\"\n\n";

local $" = "mv  ";
foreach my $size (sort {$b <=> $a} keys %files) {
  next unless @{$files{$size}} > 1;
  my %md5;
  foreach my $file (@{$files{$size}}) {
    open(FILE, $file) or next;
    binmode(FILE);
    push @{$md5{Digest::MD5->new->addfile(*FILE)->hexdigest}},
    "\"$file\" \"$duplicate_save_dir\" || shellout \"mv $file\"\n";
  }
  foreach my $hash (keys %md5) {
    next unless @{$md5{$hash}} > 1;
    print "# size: Original file $size @{$md5{$hash}}\n";
    $wasted += $size * (@{$md5{$hash}} - 1);
  }
}

1 while $wasted =~ s/^([-+]?\d+)(\d{3})/$1,$2/;
print "# $wasted bytes in duplicated files\n";

sub check_file {
  -f && push @{$files{(stat(_))[7]}}, $File::Find::name;
}

